package com.slf.flow.core.executor.function;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.slf.flow.executor.FunctionExecutor;
import com.slf.flow.annotation.Comment;
import com.slf.flow.annotation.Example;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;

@Component
@Comment("常用加密Hashlib常用方法")
public class HashlibFunctionExecutor implements FunctionExecutor {

    @Override
    public String getFunctionPrefix() {
        return "hashlib";
    }

    @Comment("hashlib加密")
    @Example("${hashlib.string(resp.html)}")
    public static String string(String ct, String ha, String bts0, String bts1, String chars) {
        String data = getData(ct, ha, bts0, bts1, chars);
        return data;
    }

    @Comment("hashlib加密goJSON")
    @Example("${hashlib.goJSON(jsonObject)}")
    public static String goJSON(JSONObject jsonObject) {
        String data = getData(jsonObject);
        return data;
    }
    @Comment("hashlib加密go")
    @Example("${hashlib.go(ct, ha, bts0, bts1, chars)}")
    public static String go(String ct, String ha, String bts0, String bts1, String chars) {
        String data = getData(ct, ha, bts0, bts1, chars);
        return data;
    }

//    @Comment("hashlib加密")
//    @Example("${hashlib.string(resp.bytes)}")
//    public static String string(byte[] bytes){
//        return DigestUtils.hashlibHex(bytes);
//    }
//
//    @Comment("hashlib加密")
//    @Example("${hashlib.string(resp.stream)}")
//    public static String string(InputStream stream) throws IOException {
//        return DigestUtils.hashlibHex(stream);
//    }
//
//    @Comment("hashlib加密")
//    @Example("${hashlib.bytes(resp.html)}")
//    public static byte[] bytes(String str){
//        return DigestUtils.hashlib(str);
//    }
//
//    @Comment("hashlib加密")
//    @Example("${hashlib.bytes(resp.bytes)}")
//    public static byte[] bytes(byte[] bytes){
//        return DigestUtils.hashlib(bytes);
//    }
//
//    @Comment("hashlib加密")
//    @Example("${hashlib.bytes(resp.stream)}")
//    public static byte[] bytes(InputStream stream) throws IOException {
//        return DigestUtils.hashlib(stream);
//    }

    public static String getData(JSONObject jsonObject){
        String ct = jsonObject.getString("ct");
        String ha = jsonObject.getString("ha");
        JSONArray bts = jsonObject.getJSONArray("bts");
        String bts0 = bts.getString(0);
        String bts1 = bts.getString(1);
        String chars = jsonObject.getString("chars");
        String data = getData(ct, ha, bts0, bts1, chars);
        return data;
    }

    private static String getData(String ct, String ha, String bts0, String bts1, String chars) {
        for (int i = 0; i < chars.length(); i++) {
            for (int j = 0; j < chars.length(); j++) {
                String verification = bts0+ chars.charAt(i) + chars.charAt(j) + bts1;
                String md5Sha = MD5_SHA(ha, verification);
                if(md5Sha.equals(ct)){
                    System.out.println("-----------------------------");
                    System.out.println(verification);
                    System.out.println(md5Sha);
                    System.out.println(ct);
                    return verification;
                }


            }
        }
        return null;
    }


        public static String MD5_SHA(String method,String s ) {
            char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                    'a', 'b', 'c', 'd', 'e', 'f'};
            try {
                byte[] strTemp = s.getBytes();
                //如果输入“SHA”，就是实现SHA加密。
                MessageDigest mdTemp = MessageDigest.getInstance(method);
                mdTemp.update(strTemp);
                byte[] md = mdTemp.digest();
                int j = md.length;
                char str[] = new char[j * 2];
                int k = 0;
                for (int i = 0; i < j; i++) {
                    byte byte0 = md[i];
                    str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                    str[k++] = hexDigits[byte0 & 0xf];
                }
                return new String(str);
            } catch (Exception e) {
                return null;
            }
        }
        public static void main(String[] args) {
//            System.out.print(MD5_SHA("123456", "MD5"));
//            System.out.print(MD5_SHA("12345qqq6", "sha1"));
            String go = "{\"bts\":[\"1658827038.298|0|dOo\",\"jLr69jhpt8mK5C0xFEEkpI%3D\"],\"chars\":\"FsxJiZsgIIRQXCxodyQLns\",\"ct\":\"d8071fe53297f9ba0f07785c2212aa33\",\"ha\":\"md5\",\"tn\":\"__jsl_clearance_s\",\"vt\":\"3600\",\"wt\":\"1500\"}";
//            System.out.println(getData(go));
        }



}
